<?
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?
require_once('src/facebook.php');
?>

<title> SentyFB - Choose a profile</title>
<link rel="shortcut icon" href="http://www.sentyfb.altervista.org/img/title.ico" type="image"/>
<link rel="icon" href="http://www.sentyfb.altervista.org/img/title.ico" />
</head>

<body>
<h3 align="right">
<a href="/"> Home </a>
</h3>
<h2>
	Select one profile from the list below.
</h2>


<?php
// take the fb obj, and then the user and the user profile
if (isset($_SESSION['facebook_s'])) {
  $facebook_s = $_SESSION['facebook_s'];
  $facebook = unserialize($facebook_s);
}
if ($facebook) {
  $user = $facebook->getUser();
  $user_profile = $facebook->api('/me');

  if (isset($_GET['friends'])) {
	$user_friends = $facebook->api('/me/friends');
  }
  else {
  ?>
	Get your
	<a href="list.php?friends"> friend list</a>  
  <?php
  }
	$link = "https://graph.facebook.com/".$user."/picture";
	?>
	<h2>
	<center>
	<img src="<? echo $link?>" width="60" height="60" align="middle" border="2"/>
	&nbsp
	<a href="show.php?id=<? echo $user ?>"> Me </a>   
	</center>
	</h2>
  <?
  if (isset($_GET["friends"]))
  {
  ?>
	<br  />
	<br  />
	<font size="+1" color="#FF0000"> Your friends </font>
	<br  />
	<br  />
	<table border=1> 
  
	<?	
	asort($user_friends['data']);
	$k = 0;
	foreach ($user_friends['data'] as $i) {
	  $arrayy[$k] = $i;
	  $k++;
	}
	
	$num_of_friends = count($user_friends['data']);
	
  //  echo "num of friends: ".$num_of_friends."<br />";
	$num_of_cols = 4;
	$num_of_rows = intval($num_of_friends/$num_of_cols);
	if ($num_of_rows % $num_of_cols != 0)
	  $num_of_rows++;
  //  echo 'num of rows: '.$num_of_rows.'<br />';
	$count = 0;
	
	for ($i = 0; $i < $num_of_rows; $i++) {
	  for ($j = 0; $j < $num_of_cols; $j++) {
		if ($count == 0)
		  echo '<tr>';
		else if ($count % $num_of_cols == 0)
		  echo '</tr><tr>';
  
		if ($num_of_rows * $j + $i < $num_of_friends) {
  
		  $elem = $arrayy[$num_of_rows * $j + $i];
	  
			$link = "https://graph.facebook.com/".$elem['id']."/picture";  
			?>
		  
			<td><img src="<? echo $link?>" width="40" height="40" align="middle" border="1"/>
			&nbsp
			<a href="show.php?id=<? echo $elem['id'] ?>"> <? echo $elem['name'] ?> </a> </td>  
			<?
		}
		else{
		?> <td> &nbsp </td> 
	 <? }
			$count++;
		 
	  }
	}
  }
  ?>
  
  </tr>
  </table>
<?
}
else {
  $facebook = null;
  echo "FB obj not found.<br />";
  echo "Redirecting to the home page. Please wait just a few seconds";
  ?>
  <META HTTP-EQUIV="refresh" content="2 http://www.sentyfb.altervista.org/"> 
  <?
}
?>

</body>
</html>